/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vivo.ic.dm;

import android.content.Context;

import com.vivo.ic.VLog;

/**
 * Runs an actual download
 */
public class DownloadThread extends Thread {
    private static final String TAG = Constants.PRE_TAG + "DownloadThread";
    
    private final Context mContext;
    private final DownloadInfo mInfo;
    public DownloadThread(Context context, DownloadInfo info) {
        mContext = context;
        mInfo = info;
    }
    
    /**
     * Executes the download in a separate thread
     */
    @Override
    public void run() {
        logInfo("DownloadThread run(), info.title = " + mInfo.mTitle);

        // 防止一个下载任务被两个线程同时下载
        if (DMUtil.isIdDownloading(mInfo.mId)) {
            logInfo("vsp id " + mInfo.mId + " has already been downloading");
            return;
        }
        logInfo("vsp record " + mInfo.mId + " downloading");
        DMUtil.addDownloadingId(mInfo.mId);

        new ChildDownloadManager(mContext, mInfo).startDownload();
        logInfo("DownloadThread is over");
        DMUtil.removeDownloadingId(mInfo.mId);

    }

    private void logInfo(String msg) {
        VLog.i(TAG, "[" + mInfo.mId + "] " + msg);
    }
}
